<?php

ini_set('error_reporting', E_ERROR);

	/**
	 *
	 * @param type $images
	 * @return \stdClass 
	 */
	function processImagesElement($images)
	{
		$attr = $images->attributes();
		
		$newImage = new stdClass;
		$newImage->representative	= (string)$attr->representative;
		$newImage->fps				= (string)$attr->fps;
		$newImage->u				= (string)$attr->u;
		$newImage->v				= (string)$attr->v;
		$newImage->w				= (string)$attr->w;
		$newImage->h				= (string)$attr->h;
		$newImage->areas			= array();
		
		foreach($images->image->rect as $rect)
		{
			$area = processImageArea($rect);
			$newImage->areas[]= $area;
		}
		
		return $newImage;
	}
	/**
	 *
	 * @param type $rect
	 * @return \StdClass 
	 */
	function processImageArea($rect)
	{
		$attr = $rect->attributes();
		
		$area = new StdClass;
		$area->type		= (string)$attr->type;
		$area->score	= (string)$attr->score;
		$area->u		= (string)$attr->u;
		$area->v		= (string)$attr->v;
		$area->w		= (string)$attr->w;
		$area->h		= (string)$attr->h;
		
		return $area;
	}
	/**
	 *
	 * @param type $area
	 * @return \stdClass 
	 */
	function processNoFaceArea($area)
	{
		$attr = $area->attributes();
		
		$newArea = new stdClass;
		$newArea->id			= (string)$attr->id;
		$newArea->srcType		= (string)$attr->source_type;
		$newArea->orientation	= (string)$attr->orientation;
		$newArea->u				= (string)$attr->u;
		$newArea->v				= (string)$attr->v;
		$newArea->w				= (string)$attr->w;
		$newArea->h				= (string)$attr->h;
		
		return $newArea;
	}
	/**
	 *
	 * @param SimpleXMLElement $koma 
	 */
	function processKomaElement($koma)
	{
		$attr = $koma->attributes();
		
		$newKoma = new stdClass;
		$newKoma->id		= (string)$attr->string;
		$newKoma->index		= (string)$attr->index;
		$newKoma->u			= (string)$attr->u;
		$newKoma->v			= (string)$attr->v;
		$newKoma->w			= (string)$attr->w;
		$newKoma->h			= (string)$attr->h;
		$newKoma->video		= (string)$attr->video;
		$newKoma->timestamp	= (string)$attr->time_stamp;
		$newKoma->imageMeta	= (string)$attr->image_meta;
		$newKoma->audioMeta	= (string)$attr->audio_meta;
		$newKoma->fileUri	= (string)$attr->original_file_uri;

		// Transform the Image URI for now
		$newKoma->fileUri = str_replace("/var/local/working_dir/1207/java/SAMPLE", "/canvasStuff/images", $newKoma->fileUri);
		
		// Process Koma images
		$newKoma->images	= processImagesElement($koma->images);
		$newKoma->noFace	= processNoFaceArea($koma->no_face_area);
		
		return $newKoma;
	}
	
	function processPageElement($page)
	{
		$attr = $page->attributes();
		
		$newPage = new stdClass;
		$newPage->index	= (string)$attr->index;
		
		return $newPage;
	}
	
	$pages = array();
	
	$layout_file = dirname(__FILE__) . '/layout.xml';
	$xmlDoc = simplexml_load_file($layout_file);

	// One page at a time
	foreach($xmlDoc->page as $page)
	{
		$currentPage = processPageElement($page);
		$currentPage->komas = array();
		
		foreach($page->koma as $i => $koma)
		{
			//var_dump($koma);
			$currentPage->komas[]= processKomaElement($koma);
		}
		$pages[]= $currentPage;
	}
	header("Content-type: application/json");
	echo json_encode($pages);
	
	
?>